// components/b-message/b-message.js
Component({
	options: {
		"styleIsolation": "apply-shared"
	},
	/**
	 * 组件的属性列表
	 */
	properties: {
		show: {
			type: Boolean,
			value: false
		},
		content: {
			type: String,
			value: ''
		}
	},

	/**
	 * 组件的初始数据
	 */
	data: {
		translateY: 'translateY(-30px)',
		opacity: 0,
		type: 'successMessage',
		lastTime: 0
	},
	/**
	 * 组件的方法列表
	 */
	methods: {
		showFn(type, text){
			var date = new Date();
			let toTime = date.getTime()
			let time = parseInt(2000 - (toTime - this.data.lastTime))
			this.setData({
				lastTime: toTime
			})
			const that = this
			function showChange(){
				that.setData({
					show: true,
					content: text,
					type
				})
				setTimeout(() => {
					that.setData({
						translateY: 'translateY(0)',
						opacity: 1
					})
				},10)
				setTimeout(() => {
					that.setData({
						translateY: 'translateY(-30px)',
						opacity: 0
					})
				}, 1500)
				setTimeout(() => {
					that.setData({
						show: false,
						content: ''
					})
				}, 2000)
			}
			if(this.data.show){
				setTimeout(() => {
					showChange()
				}, (time+ 200))
			} else {
				showChange()
			}
		}
	},
})
function getContext() {
	const pages = getCurrentPages();
	return pages[pages.length - 1];
}

const message = new Object
message.success = (text) => {
	const context = getContext();
	const that = context.selectComponent('#message');
	that.showFn('successMessage', text)
}
message.error = (text) => {
	const context = getContext();
	const that = context.selectComponent('#message');
	that.showFn('errorMessage', text)
}
message.waring = (text) => {
	const context = getContext();
	const that = context.selectComponent('#message');
	that.showFn('waringMessage', text)
}

export default message